package cn.Liujialin.basic.service.impl;


import cn.Liujialin.basic.mapper.BeanMapper;
import cn.Liujialin.basic.query.BeanQuery;
import cn.Liujialin.basic.service.IBeanService;
import cn.Liujialin.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// @Service  当前类是基本的CRUD的实现，应该交给真正的业务类继承
// 它不应该交给Spring容器管理

//只读事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BeanServiceImpl<T> implements IBeanService<T> {

    //T  一定要加T，在真正的业务实现来中指明操作的实体
    @Autowired
    private BeanMapper<T> mapper;


    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        mapper.del(id);
    }

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public List<T> findByAll() {
        return mapper.findByAll();
    }

    @Override
    public PageList<T> queryPage(BeanQuery query) {
        Long total = mapper.queryCount(query);
        List<T> rows = mapper.queryData(query);

        return new PageList<>(total, rows);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        mapper.patchDelete(ids);
    }
}
